package com.uchengwang.ui.cm_ui.uu.adapter;

import android.app.Activity;
import android.content.Context;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import com.uchengwang.R;
import com.uchengwang.functions.http.config.BaseConfig;
import com.uchengwang.functions.image.ImageManager;
import com.uchengwang.ui.cm_ui.cycle.adapter.BaseCycleAdapter;
import com.uchengwang.ui.cm_ui.cycle.viewHolder.BaseViewHolder;
import com.uchengwang.ui.cm_ui.login.bean.UserBean;
import com.uchengwang.ui.cm_ui.me.presenter.HttpMeManager;
import com.uchengwang.ui.utils.UCommonUtil;
import com.uchengwang.ui.utils.WidgetUtil;

import io.rong.imlib.model.Conversation;

/**
 * Created by U城网 on 2017/6/3.
 * 添加朋友
 */

public class AddAdapter extends BaseCycleAdapter<UserBean, RecyclerView.ViewHolder>{
    private Context context;

    public AddAdapter(Context context){
        this.context = context;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_layout_view, parent, false);
        return new BaseViewHolder(view, 0);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder h, final int position) {
        final BaseViewHolder holder = (BaseViewHolder) h;
        final UserBean bean = list.get(position);
        final boolean isFriend = bean.isFriend();//是否是好友
        final boolean isFollow = bean.isFollow();//是否关注

        String headUrl = bean.getUserAvatar();
        if (TextUtils.isEmpty(headUrl)){
            ImageManager.getInstance().loadImage(context, holder.head, R.drawable.no_head, ImageManager.ImageType.CIRCLE);
        }else {
            ImageManager.getInstance().loadImage(context, holder.head, headUrl, ImageManager.ImageType.CIRCLE);
        }

        String remark = bean.getRemarkName();
        String realName = bean.getRealName();
        if (TextUtils.isEmpty(remark)){
            if (!TextUtils.isEmpty(realName)){
                holder.name.setText(realName);
            }else {
                holder.name.setText(bean.getNickName());
            }
        }else {
            holder.name.setText(remark);
        }

        if (!TextUtils.isEmpty(bean.getUniName())){
            holder.des.setText(bean.getUniName());
        }else {
            holder.des.setText("暂无学校信息");
        }
//        holder.des.setText(bean.getUserSign());

        if (bean.getId() == UCommonUtil.getUtil().getId()){
            holder.bt.setVisibility(View.GONE);
        }else {
            holder.bt.setVisibility(View.VISIBLE);
            if (isFriend){
                //是好友关系
                WidgetUtil.getUtil().setChatWidget(holder.bt);
            }else {
                holder.bt.setBackgroundResource(0);
                WidgetUtil.getUtil().setAttenWidget(holder.bt, isFollow);
            }
        }

        if (bean.getUserRole() == BaseConfig.STUDENT_ROLE){
            holder.apply_sex.setVisibility(View.VISIBLE);
            if (bean.getSex() ==0){
                ImageManager.getInstance().loadImage(context, holder.apply_sex, R.drawable.icon_woman, ImageManager.ImageType.NARMOL);
            }else {
                ImageManager.getInstance().loadImage(context, holder.apply_sex, R.drawable.icon_man, ImageManager.ImageType.NARMOL);
            }
        }else {
            holder.apply_sex.setVisibility(View.GONE);
        }


//        if (bean.isUni()){
//            holder.schoolBa_uni.setVisibility(View.VISIBLE);
//        }else {
//            holder.schoolBa_uni.setVisibility(View.GONE);
//        }
//
//        if (bean.isOrgAdmin()){
//            holder.schoolBa_org.setVisibility(View.VISIBLE);
//        }else {
//            holder.schoolBa_org.setVisibility(View.GONE);
//        }


        holder.bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (isFriend){
                    //是好友关系 聊天
                    UCommonUtil.getUtil().toChatActiviy(context, bean.getId(), Conversation.ConversationType.PRIVATE, bean.getNickName());

                }else{//关注/取消关注
                    follow(holder.bt, bean);
                }
            }
        });

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                UCommonUtil.getUtil().toUserActivity((Activity) context, bean.getId());
            }
        });
    }

    /**
     * 关注/取消关注
     * */
    private void follow(final Button bt, final UserBean bean){
        HttpMeManager.getPresenter().addAtten((Activity) context, !bean.isFollow(), bean.getId(), new HttpMeManager.AttenListener() {
            @RequiresApi(api = Build.VERSION_CODES.M)
            @Override
            public void atten(int status) {
                if (status != 0){
                    UCommonUtil.getUtil().showToast(context, "操作失败");
                    return;
                }
                if (status == 0){
                    if (bean.isFollow()){
                        UCommonUtil.getUtil().showToast(context, "取消关注成功");
                        bean.setFollow(false);
                        WidgetUtil.getUtil().setAttenWidget(bt, false);
                    }else {
                        UCommonUtil.getUtil().showToast(context, "关注成功");
                        bean.setFollow(true);
                        WidgetUtil.getUtil().setAttenWidget(bt, true);
                    }
                }


            }
        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

//    class AddVh extends RecyclerView.ViewHolder{
//        private View view;
//        private ImageView head, imageAtten;
//        private TextView name, des;
//        private Button bt;
//        private ImageView apply_sex;
//
//        public AddVh(View itemView) {
//            super(itemView);
//
//            AutoUtils.autoSize(itemView);
//
//            view = itemView;
//            head = (ImageView) itemView.findViewById(R.id.apply_image);
//            name = (TextView) itemView.findViewById(R.id.apply_name);
//            des = (TextView) itemView.findViewById(R.id.apply_school);
//
//            imageAtten = (ImageView) itemView.findViewById(R.id.apply_authen_logo);
//            bt = (Button) itemView.findViewById(R.id.apply_atten);
//            bt.setVisibility(View.VISIBLE);
//
//            apply_sex = (ImageView) itemView.findViewById(R.id.apply_sex);
//            apply_sex.setVisibility(View.VISIBLE);
//        }
//    }
}
